﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.DataAccessHelper;
using DeviceInfoManage.DataAccessLayer;
/// <summary>
/// Department 的摘要说明
/// </summary>
public class Department
{
    private int departmentId;           /*部门编号*/
    private string departmentName;      /*部门名称*/
    private string errMessage;          /*保存操作的错误信息*/

    public string ErrMessage
    {
        get { return errMessage; }
        set { errMessage = value; }
    }

	public Department()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    /*根据部门编号得到部门名称*/
    public string GetDepartMentName(int departmentId)
    {
        string quertString = "select departmentName from departmentInfo where departmentId = " + departmentId;
        DataSet ds = (new DataBase()).GetDataSet(quertString);
        return ds.Tables[0].Rows[0]["departmentName"].ToString();
    }

    /*得到所有的部门信息*/
    public DataSet GetDepartMentName()
    {
        string queryString = "select * from departmentInfo";
        DataBase db = new DataBase();
        return db.GetDataSet(queryString);
    }

    /*添加新的设备类别*/
    public bool AddDepartMentName(string dptName)
    {
        /*首先判断系统中是否已经存在了该部门名称*/
        DataBase db = new DataBase();
        string queryString = "select * from departmentInfo where departmentName=" + SqlString.GetQuotedString(dptName);
        if (db.GetRecord(queryString))
        {
            this.errMessage = "该部门名称系统已经存在!";
            return false;
        }
        /*如果系统中不存在该类别,就可以执行新的部门名称的加入*/
        string insertString = "insert into departmentInfo(departmentName) values(" + SqlString.GetQuotedString(dptName) + ")";
        if (db.InsertOrUpdate(insertString) < 0)
        {
            this.errMessage = "新部门信息加入数据库时发生了错误！";
            return false;
        }
        return true;
    }

    /*修改已经存在的部门*/
    public bool UpdateDepartMentName(int dptId, string dptName)
    {
        /*首先判断系统中是否已经存在了该部门名称*/
        DataBase db = new DataBase();
        string queryString = "select * from departmentInfo where departmentName =" + SqlString.GetQuotedString(dptName);
        if (db.GetRecord(queryString))
        {
            this.errMessage = "该部门名称系统已经存在!";
            return false;
        }
        /*然后执行根据id更新部门名称信息*/
        string updateString = "update departmentInfo set departmentName =" + SqlString.GetQuotedString(dptName);
        updateString += " where departmentId=" + dptId.ToString();
        if (db.InsertOrUpdate(updateString) < 0)
        {
            this.errMessage = "修改部门名称时发生了错误！";
            return false;
        }
        return true;
    }

    /*删除某个分类编号的设备分类信息*/
    public bool DeleteDepartMentName(int dptId)
    {
        /*首先判断该分类下是否还有设备，如果有是不能执行删除的*/
        DataBase db = new DataBase();
        string queryString = "select * from deviceInfo where departmentName=" + dptId;
        if (db.GetRecord(queryString))
        {
            this.errMessage = "该部门下还存在设备信息，不能执行删除操作!";
            return false;
        }

        string deleteString = "delete from departmentInfo where departmentId=" + dptId;
        if (db.InsertOrUpdate(deleteString) < 0)
        {
            this.errMessage = "删除部门信息时发生了错误!";
            return false;
        }
        return true;
    }
}
